//==========================================================================
// Copyright (c) 2000-2008,  Elastos, Inc.  All Rights Reserved.
//==========================================================================

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <errno.h>
#include <sys/time.h>

#include <aura.h>

#define MY_CHANNEL "MyCentral"
#define BUFSIZE 4096
#define TIMES 10

EXTERN_C void AnsiToUnic(
            /* [out] */ Aura_PWChar desc,
            /* [in] */ char *src)
{
    while (*src) {
        *desc++ = (Aura_WChar)*src++;
    }
    *desc = 0;
}

int main(void)
{
    Aura_ChannelId id;
    Aura_Int32 i;
    Aura_ECode ec;
    Aura_WChar wtmp[64];

    char chBuf[BUFSIZE];
    char reqBuf[BUFSIZE];

    AnsiToUnic(wtmp,MY_CHANNEL);
    ec = AuraIpcChannel_Connect(wtmp, &id);
    if (AURA_FAILED(ec)) {
        printf("AuraIpcChannel_Connect failed!\n");
        return AURA_E_FAIL;
    }
    srand((int)time(0));

    for (i = 0; i < TIMES; i++) {
        sprintf(reqBuf, "Client Thread %d, %d",getpid(), i);

        ec = AuraIpcChannel_SyncWrite(id, reqBuf, BUFSIZE, chBuf, BUFSIZE);
        if (AURA_FAILED(ec)) {
            printf("AuraIpcChannel_SyncWrite failed!!!\n");
            goto Exit;
        }
        else
           printf("Receive Message: %s\n", chBuf);

        AuraThread_Sleep(1000, NULL);

    }

    reqBuf[0] = 0;

    ec = AuraIpcChannel_AsyncWrite(id, reqBuf, BUFSIZE);

Exit:
    if (-1 != id)
        AuraIpcChannel_Disconnect(id);

    return 0;
}
